package io.renren.membermanage.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.crypto.digest.DigestUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.constant.BizSystemException;
import io.renren.enums.ResultCodeEnum;
import io.renren.membermanage.dto.RegisterParam;
import io.renren.membermanage.entity.SysUser;
import io.renren.membermanage.mappper.SysUserMapper;
import io.renren.membermanage.service.SysUsersService;
import io.renren.util.PasswordUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class SysUsersServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements SysUsersService {

    @Value("${aes.key}")
    private String aesKey;

    @Override
    public SysUser verify(String memberName, String memberPassword) {
        try {
            QueryWrapper<SysUser> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("username", memberName);
            queryWrapper.eq("status", 1);
            SysUser one = this.getOne(queryWrapper);
            if(null == one){
                throw new BizSystemException(ResultCodeEnum.ADMIN_ACCOUNT_NOT_EXIST_ERROR.getCode(), ResultCodeEnum.ADMIN_ACCOUNT_NOT_EXIST_ERROR.getMessage());
            }
            if (!PasswordUtils.matches(memberPassword, one.getPassword())) {
                throw new BizSystemException(ResultCodeEnum.ADMIN_ACCOUNT_ERROR.getCode(), ResultCodeEnum.ADMIN_ACCOUNT_ERROR.getMessage());
            }
            return one;
        } catch (Exception e) {
            log.error("验证用户登入时报错:{}", e.getMessage());
            return null;
        }
    }

    @Override
    public Long addMember(RegisterParam param) {
        //校验账户唯一性
        QueryWrapper<SysUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", param.getMemberName());
        queryWrapper.eq("status", 1);
        SysUser one = this.getOne(queryWrapper);
        if(null != one){
            throw new BizSystemException(ResultCodeEnum.ADMIN_ACCOUNT_EXIST_ERROR.getCode(), ResultCodeEnum.ADMIN_ACCOUNT_EXIST_ERROR.getMessage());
        }
        //校验密码规则：一个大写字母，一个小写字母，1个特殊字符，1个数字，且不能有中文，长度为8到12位
        String regex = "^(?=.*[A-Z])(?=.*[a-z])(?=.*\\\\d)(?=.*[^A-Za-z0-9]).{8,12}$";
        if(!param.getMemberPassword().matches(regex)){
            throw new BizSystemException(ResultCodeEnum.ADMIN_PASSWORD_FORMAT_ERROR.getCode(), ResultCodeEnum.ADMIN_PASSWORD_FORMAT_ERROR.getMessage());
        }
        try {
            SysUser member = new SysUser();
            member.setId(IdWorker.getId());
            member.setUsername(param.getMemberName());
            member.setPassword(PasswordUtils.encode(param.getMemberPassword()));
            member.setCreator(member.getId());
            this.baseMapper.insert(member);
            return member.getId();
        } catch (Exception e) {
            throw new BizSystemException(ResultCodeEnum.SERVICE_ERROR.getCode(), ResultCodeEnum.SERVICE_ERROR.getMessage());
        }
    }

    public List<Long> getUserSameDept(Long userId){
        return this.baseMapper.getUserSameDept(userId);
    }
}
